<template>
  <div class="allMembers">
    <div class="search">
      <div>
        <label>搜索：</label>
      </div>

      <el-input v-model="listParams.keyword" placeholder="ID/昵称"></el-input>
      <div>
        <el-button @click='searchKeyWord' type="primary">搜索</el-button>
      </div>
      <div>
        <el-button @click="addMarketPersonnal" type="primary">添加</el-button>
      </div>
    </div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="id" width="90">
        <template slot-scope="scope">
          <span @click="userDetails(scope.row)" class="link-type">{{scope.row.id}}</span>
        </template>
      </el-table-column>
      <el-table-column label="头像" width="90">
        <template slot-scope="scope">
          <div class="imgBox">
            <img :src="scope.row.avatar" alt>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column label="注册时间">
        <template slot-scope="scope">
          <!-- {{ scope.row.time | parseTime('{y}-{m}-{d} {h}:{i}') }} -->
          <span>{{scope.row.timer}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="identity" label="身份"></el-table-column>
      <el-table-column prop="accreditation" label="是否认证"></el-table-column>
      <el-table-column label="推荐人">
        <template slot-scope="scope">
          <span class="link-type">{{scope.row.commender}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="membership" label="直推会员人数"></el-table-column>
      <el-table-column prop="classTwoMembership" label="二级会员人数"></el-table-column>
      <el-table-column prop="orderNumber" label="订单数"></el-table-column>
      <el-table-column label="余额">
        <template slot-scope="scope">
          <span @click="openbillDetails(item)" class="link-type">{{scope.row.balance}}</span>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination"
      background
      @current-change="handleCurrentChange"
      layout="prev, pager, next"
      :total="total"
    ></el-pagination>
    <!-- 添加业务员 -->
    <el-dialog title="添加" :visible.sync="addMarketDialogVisible" width="20%">
      
      <el-form ref="form" label-width="80px">
        <el-form-item label="用户ID">
            <el-input v-model="form.userId" placeholder="请输入用户ID"></el-input>
        </el-form-item>
        <el-form-item label="推荐人ID">
            <el-input v-model="form.recommenderId" placeholder="请输入经销商ID"></el-input>
        </el-form-item>

      </el-form>    
      <span slot="footer" class="dialog-footer">
        <el-button @click="addMarketDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="sureAddMarket()">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 余额明细 -->
    <billDetails ref="billDetails"></billDetails>
  </div>
</template>


<script>
import {ajax} from '@/api';
import {getToken} from "@/utils/auth";
import billDetails from "./components/billDetails";
export default {
  name: "salesman",
  data() {
    return {
      total:0,
      addMarketDialogVisible:false,
      input: "",
      form:{
          userId:'',
          recommenderId:''
      },
      tableData: [
        {
          id: 2568,
          avatar:
            "https://ss1.baidu.com/9vo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=8d92cdd953df8db1a32e7a643922dddb/0ff41bd5ad6eddc4802878ba34dbb6fd536633a0.jpg",
          name: "王师傅",
          timer: "2018-10-12 23:45",
          identity: "业务员",
          accreditation: "是",
          commender: "王师傅",
          membership: 2,
          classTwoMembership: 5,
          orderNumber: 1,
          balance: 520
        }
      ],
      listParams:{
        role:'user',
        auth:0,
        keyword:'',
        start:0,
        limit:10,
        token:getToken()
      }
    };
  },
  methods: {
    searchKeyWord(){
      if(this.listParams.keyword === ''){
        return this.$message.error('ID/昵称不能为空');
      }
      this.getList();
    },
    addMarketPersonnal() {
        this.addMarketDialogVisible = true;
    },
    sureAddMarket(){
        this.addMarketDialogVisible = false;
    },
    openbillDetails(item) {
      this.$refs.billDetails.openBill(item);
    },
    userDetails(data) {
      this.$router.push({ name: "userDetails",params:{id:data.id}});
    },
    handleCurrentChange(val){
        this.listParams.start = (val - 1) * this.listParams.limit;
        this.getList();
    },
    //经销商人员列表
    getList(){
      ajax('users',this.listParams).then((res) => {
        if(res.status === 200){
          console.log(res);
          this.total = res.data.total;
        }
      });
    },
  },
  mounted(){
    this.getList();
  },
  components: {
    billDetails
  }
};
</script>


<style lang="less" scoped>
.allMembers {
  padding: 1rem;
  .search {
    display: flex;
    margin: 0.5rem 0 1rem 0;
    div:first-child {
      width: 3.5rem;
      line-height: 2.5rem;
      label {
        font-weight: 400;
      }
    }
    div:nth-child(2) {
      width: 11rem;
      margin-right: 1rem;
    }
    div:nth-child(3){
        margin-right: 1rem;
    }
  }
  .imgBox {
    width: 50px;
    height: 50px;
    margin: 0 auto;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50px;
    }
  }
  .pagination{
    text-align: right;
    margin: 1rem 0;
  }

}
</style>
